package com.suse.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.suse.dao.NoticeDao;
import com.suse.dto.NoticeDto;
import com.suse.pojo.Notice;
import com.suse.service.NoticeService;
import com.suse.util.ResultVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;

@Service
public class NoticeServiceImpl implements NoticeService {
    @Resource
    NoticeDao dao;
    @Override
    public ResultVo add(Notice n) {
        try{
            int num = dao.insert(n);
            if(num>0){
                return ResultVo.success("新增公告成功");
            }else {
                return ResultVo.error("新增公告失败");
            }
        }catch(Exception e){
            e.printStackTrace();
            return ResultVo.error("数据异常");
        }
    }
    @Override
    public ResultVo update(Notice n) {
        try{
            int num = dao.updateById(n);
            if(num>0){
                return ResultVo.success("公告修改成功");
            }else {
                return ResultVo.error("公告修改失败");
            }
        }catch(Exception e){
            e.printStackTrace();
            return ResultVo.error("数据异常");
        }
    }
    @Override
    public ResultVo del(Integer id) {
        try{
            int num = dao.deleteById(id);
            if(num>0){
                return ResultVo.success("删除成功");
            }else {
                return ResultVo.error("删除失败");
            }
        }catch(Exception e){
            e.printStackTrace();
            return ResultVo.error("数据异常");
        }
    }

    @Override
    public ResultVo select(NoticeDto dto){
        try{
            QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();

            boolean is = null!=dto.getNoticeTitle()&&!dto.getNoticeTitle().equals("");
            queryWrapper.like(is,"notice_title",dto.getNoticeTitle());
            boolean iss = null!=dto.getNoticeCreator()&&!dto.getNoticeCreator().equals("");
            queryWrapper.like(iss,"notice_creator",dto.getNoticeCreator());

            Page<Notice> page = new Page<>(dto.getPage(), dto.getRow());
            Page<Notice> infoPage = dao.selectPage(page,queryWrapper);
            if(!infoPage.getRecords().isEmpty()){
                HashMap<String,Object> map = new HashMap<>();
                map.put("list",infoPage.getRecords());
                map.put("total",infoPage.getTotal());
                return ResultVo.success("查询成功",map);
            }else{
                return ResultVo.error("没有数据");
            }
        }catch(Exception e){
            e.printStackTrace();
            return ResultVo.error("数据异常");
        }
    }
}
